home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Typography Samples / StyleLayoutSample ƒ / StyleLayoutSample.c next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  8.6 KB  |  337 lines  |  [TEXT/MPS ]

  1. /*
  2.     gxStyle Layout Sample Program
  3.     By Eric Mader,
  4.     After Mike Fairman, Oliver Steele et. al.
  5.  
  6.     1.0B2 (MD, 09/10/93) -- allocate a new 1 MB graphics client instead of using
  7.                             the default 600K client.
  8. */
  9.  
  10. /* Copyright ©1989, 1990, 1991 Apple Computer, Inc.  All rights reserved. */
  11.  
  12.  
  13. #ifndef THINK_C
  14. #include <Quickdraw.h>
  15. #include <Fonts.h>
  16. #include <Windows.h>
  17. #include <Dialogs.h>
  18. #include <Events.h>
  19. #include <Memory.h>
  20. #include <Menus.h>
  21. #include <String.h>
  22. #include <Desk.h>
  23. #include <Script.h>
  24. #include <ToolUtils.h>
  25. #define thePort qd.thePort
  26. #define screenBits qd.screenBits
  27. #endif
  28.  
  29. #include "graphics libraries.h"
  30. #include "qd library.h"
  31. #include "font library.h"
  32. #include "graphics debugging.h"
  33. #include "graphics routines.h"
  34. #include "graphics toolbox.h"
  35.  
  36. #include "layout feature constants.h"
  37. #include "layout types.h"
  38. #include "layout routines.h"
  39. #include "layout library.h"
  40.  
  41. /* This macro is useful for constructing fixed values */
  42. #define f(a,b) (((fixed) (a) << 16) + (b))
  43.  
  44. /* various menu and dialog things */
  45. enum {aboutDLOG = 128, appleMenuID = 128, fileMenuID, editMenuID}; /* resource ID's */
  46.  
  47. enum {okButton = 1, showsItem, authorItem};
  48.  
  49. enum {
  50.     /* Apple Menu */
  51.     aboutCommand = 1,
  52.     
  53.     /* File Menu */
  54.     quitCommand = 1,
  55.     
  56.     /* Edit Menu */
  57.     undoCommand = 1,
  58.     cutCommand,
  59.     copyCommand,
  60.     pasteCommand,
  61.     clearCommand};
  62.  
  63. WindowPtr myWindow, whichWindow;
  64. gxViewPort myPort;
  65. Rect qdWindowRect;
  66. Rect growRect = {40, 40, 32767, 32767};
  67. Rect bigRect = {-32768, -32768, 32767, 32767};
  68. gxRectangle windowRect;
  69.  
  70. MenuHandle appleMenu, fileMenu, editMenu;
  71. Boolean done = false;
  72.  
  73. gxColor colorWhite = xRGB (0xFFFF, 0xFFFF, 0xFFFF);    /* white */
  74. gxColor hsvInitial = xHSV (0, 0xFFFF, 0xFFFF); /* red */
  75.  
  76. static void ShowAboutBox ()
  77. {    GrafPtr savePort;
  78.     DialogPtr theDialog;
  79.     short itemType;
  80.     Handle itemHdl;
  81.     Rect itemRect;
  82.     short itemHit;
  83.  
  84.     GetPort(&savePort);
  85.     theDialog = GetNewDialog(aboutDLOG, nil, (WindowPtr) -1);
  86.     SetPort(theDialog);
  87.  
  88.     GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
  89.     SetIText(itemHdl, (unsigned char *) "\pRice Dream");
  90.     GetDItem(theDialog, showsItem, &itemType, &itemHdl, &itemRect);
  91.     SetIText(itemHdl, (unsigned char *) "\pText gxStyle Changes");
  92.  
  93.     do {
  94.         ModalDialog(nil, &itemHit);
  95.     } while (itemHit != okButton);
  96.  
  97.     CloseDialog(theDialog);
  98.  
  99.     SetPort(savePort);
  100. }
  101.  
  102. static void SetupMenus ()
  103. {
  104.     InsertMenu (appleMenu = GetMenu (appleMenuID), 0);
  105.     AddResMenu (appleMenu, (ResType) 'DRVR');
  106.     InsertMenu (fileMenu = GetMenu (fileMenuID), 0);
  107.     InsertMenu (editMenu = GetMenu (editMenuID), 0);
  108.     DrawMenuBar ();
  109. }
  110.  
  111. static void DoMenuCommand (long mResult)
  112. {    short theItem = LoWord (mResult), theMenuID = HiWord (mResult);
  113.     GrafPtr savePort;
  114.     Str255 daName;
  115.     
  116.     switch (theMenuID)
  117.     {    case appleMenuID:
  118.             if (theItem == aboutCommand) ShowAboutBox ();
  119.             else
  120.             {    GetItem (appleMenu, theItem, daName);
  121.                 GetPort (&savePort);
  122.                 (void) OpenDeskAcc (daName);
  123.                 SetPort (savePort);
  124.             }
  125.         break;
  126.             
  127.         case fileMenuID:
  128.             if (theItem == quitCommand) done = true;
  129.         break;
  130.             
  131.         case editMenuID:
  132.         break;
  133.     }
  134.     
  135.     HiliteMenu (0);
  136. }
  137.  
  138. static short StrLength(char *s)
  139. {short len;
  140.  
  141.     for (len = 0; *s++ != 0; len++) ;
  142.     return len;
  143. }
  144.  
  145. void main()
  146. {    EventRecord theEvent;
  147.     gxShape layout, whiteOut;
  148.     gxRunControls gxRunControls, testControls;
  149.     gxLayoutOptions gxLayoutOptions;
  150.     gxStyle glyphStyles[3];
  151.     char *text1 =      "Aetna ";
  152.                                             /* The following is "Arabic Macintosh" in Arabic: */
  153.                                             /* meem, alif, kaf,  noon, tah,  wau,  shin, */
  154.                                             /* <sp>, alif, lam,  ein,  reh,  beh,  yeh */
  155.     static char text2[] =    {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4,
  156.                                                  0x20, 0xC7, 0xE4, 0xD9, 0xD1, 0xC8, 0xEA, 0};
  157.     char *text3 =         " Office AWAY.";
  158.     char *textRuns[3];
  159.     short level0, textLengths[3], totalLength;
  160.     gxPoint posn;
  161.     GDHandle max;
  162.     short mbh = GetMBarHeight ();
  163.     gxGraphicsClient ourClient;
  164.     
  165.     MaxApplZone(); MoreMasters(); MoreMasters();
  166.  
  167.     ourClient = GXNewGraphicsClient(0L, 1024 * 1024, 0L);
  168.     if (ourClient != NULL) {
  169.  
  170.         SetGraphicsLibraryErrors();
  171.         SetGraphicsLibraryNotices ();
  172.     /*GXSetValidation(gxInternalValidation | gxStructureValidation | gxNoMemoryManagerValidation);    /* uncomment this for less speed and more error-checking */
  173.     
  174.         InitCommonColors ();
  175.         
  176.         InitGraf(&thePort);
  177.         InitFonts();
  178.         InitWindows();
  179.         InitMenus ();
  180.         InitCursor();
  181.     
  182.         SetupMenus ();
  183.         
  184.         /* find the deepest monitor, and make a window that just covers it */
  185.         max = GetMaxDevice (&bigRect);
  186.         qdWindowRect = (**max).gdRect;
  187.         
  188.         /* bring it down one mbh for the header, maybe another if on main screen */
  189.         if (qdWindowRect.top == 0 && qdWindowRect.left == 0) qdWindowRect.top += mbh;
  190.         qdWindowRect.top += mbh;
  191.             
  192.         InsetRect (&qdWindowRect, 4, 4);
  193.         ShortRectToFixed (&qdWindowRect, &windowRect);
  194.         
  195.         myWindow = NewWindow(nil, &qdWindowRect, (unsigned char *) "\pStyle Layout Sample",
  196.                  true, documentProc, (WindowPtr) -1L, true, 0L);
  197.         
  198.         myPort = GXNewWindowViewPort (myWindow);
  199.         SetDefaultViewPort (myPort);
  200.     
  201.         /*
  202.             When we want to erase the whole window, we just GXDrawShape (whiteOut).
  203.          */
  204.         whiteOut = GXNewShape (gxFullType);
  205.         GXSetShapeColor (whiteOut, &colorWhite);
  206.     
  207.         /* gxInitialize the textRuns array */
  208.         textRuns[0] = text1;
  209.         textRuns[1] = text2;
  210.         textRuns[2] = text3;
  211.         
  212.         /* gxInitialize the textLengths array */
  213.         textLengths[0] = StrLength (text1);
  214.         textLengths[1] = StrLength (text2);
  215.         textLengths[2] = StrLength (text3);
  216.         
  217.         totalLength = textLengths[0] + textLengths[1] + textLengths[2];
  218.         
  219.         /* Make a default gxLayoutOptions and gxRunControls */
  220.         InitializeLayoutOptions (&gxLayoutOptions);
  221.         InitializeRunControls (&gxRunControls);
  222.         InitializeRunControls (&testControls);
  223.         
  224.         
  225.         /*
  226.             Position the layout half way down the left edge of the window. Set 
  227.             the layout's width to the window's width and set the flushness to 1/2, this
  228.             will cause the layout to center in the window.
  229.         */
  230.         gxLayoutOptions.width = windowRect.right - windowRect.left;
  231.         gxLayoutOptions.flush = fract1/2;
  232.         posn.x = 0;
  233.         posn.y = (windowRect.bottom - windowRect.top) / 2;
  234.         
  235.         /* run 0 is 38 pt. Helvetica */
  236.         glyphStyles[0] = NewLayoutStyle((char *) "\pHelvetica", ff(38), 0, &gxRunControls, nil, 0, nil);
  237.         /* SetStyleCommonFace (glyphStyles[0], italic); */
  238.     
  239.     
  240.         /* run 1 is 38 pt. Baghdad (Arabic) */
  241.         glyphStyles[1] = NewLayoutStyle((char *) "\pBaghdad Plain", ff(38), 0, &gxRunControls, nil, 0, nil);
  242.         
  243.         /* run 2 is 38 pt. Times */
  244.         glyphStyles[2] = NewLayoutStyle((char *) "\pTimes Roman", ff(38), 0, &testControls, nil, 0, nil);
  245.         
  246.         /* Build the layout. */
  247.         
  248.         layout = GXNewLayout(
  249.             3,
  250.             textLengths,
  251.             (void *) textRuns,
  252.             3,
  253.             textLengths,
  254.             glyphStyles,
  255.             1,
  256.             &totalLength,
  257.             &level0,
  258.             &gxLayoutOptions,
  259.             &posn);
  260.         GXCacheShape(layout);
  261.         testControls.flags |= gxImposeRightToLeft;
  262.         GXSetStyleRunControls(glyphStyles[2], &testControls);
  263.         GXDrawShape (layout);
  264.     
  265.     
  266.         /* Now just spin in a simple event loop until it's time to go */
  267.         while (!done)
  268.         {
  269.             if (WaitNextEvent(everyEvent, &theEvent, 0, nil))
  270.             {
  271.                 switch(theEvent.what)
  272.                 {
  273.                     case mouseDown:
  274.                     switch (FindWindow(theEvent.where, &whichWindow))
  275.                     {    case inSysWindow:
  276.                             SystemClick(&theEvent, whichWindow);
  277.                             break;
  278.                         
  279.                         case inMenuBar:
  280.                             DoMenuCommand (MenuSelect (theEvent.where));
  281.                         break;
  282.                             
  283.                         case inDrag:
  284.                             DragWindow(whichWindow, theEvent.where, &screenBits.bounds);
  285.                         break;
  286.     
  287.                         case inGrow:
  288.                         {    register long newSize;
  289.     
  290.                             newSize = GrowWindow(whichWindow, theEvent.where, &growRect);
  291.                             SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), true);
  292.                         }
  293.                         break;
  294.                         
  295.                         case inGoAway:
  296.                             if (TrackGoAway(whichWindow, theEvent.where)) done = true;
  297.                         break;
  298.     
  299.                         case inContent:
  300.                             if (whichWindow != FrontWindow())
  301.                                 SelectWindow(whichWindow);
  302.                         break;
  303.                     }
  304.                     break;
  305.     
  306.                     case keyDown:
  307.                     case autoKey:
  308.                         if (myWindow == FrontWindow () && theEvent.modifiers & cmdKey)
  309.                             DoMenuCommand (MenuKey (theEvent.message & charCodeMask));
  310.                     break;
  311.     
  312.                     case updateEvt:
  313.                         BeginUpdate((WindowPtr)theEvent.message);
  314.                         GXDrawShape (whiteOut);
  315.                         testControls.flags &= ~gxDirectionOverrideMask;
  316.                         GXSetStyleRunControls(glyphStyles[2], &testControls);
  317.                         GXCacheShape(layout);
  318.                         testControls.flags |= gxImposeRightToLeft;
  319.                         GXSetStyleRunControls(glyphStyles[2], &testControls);
  320.                         GXDrawShape (layout);
  321.                         EndUpdate((WindowPtr)theEvent.message);
  322.                     break;
  323.                 }
  324.             }
  325.         }
  326.         /* dispose everything we've allocated. */
  327.         GXDisposeShape(layout);
  328.         GXDisposeStyle (glyphStyles[0]); GXDisposeStyle (glyphStyles[1]);
  329.         GXDisposeStyle (glyphStyles[2]);
  330.         DisposeWindow(myWindow);
  331.         GXDisposeShape (whiteOut);
  332.         
  333.         DisposeCommonColors ();
  334.         GXDisposeGraphicsClient(ourClient);
  335.     }    
  336. }
  337.